package com.heer.gymnasium.module.store.views;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.heer.amap.utils.ToastUtil;
import com.heer.gymnasium.R;
import com.heer.gymnasium.api.ResponseData.ApiResponse;
import com.heer.gymnasium.api.callback.BaseCallback;
import com.heer.gymnasium.base.BaseRefreshActivity;
import com.heer.gymnasium.module.common.beans.PeriodBean;
import com.heer.gymnasium.module.store.beans.CourseBean;
import com.heer.gymnasium.module.store.beans.CourseResponseBean;
import com.zhihuiyun.library.commonadapter.QuickTypeAdapter;
import com.zhihuiyun.library.commonadapter.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 集体课详情
 * Created by zy on 2018/1/25.
 */

public class CourseActivity extends BaseRefreshActivity {
    private final static int RES_ID_ROOT = R.layout.layout_course;
    private final static int RES_ID_LIST = R.id.layout_course_lv;
    private final static int RES_ID_LIST_ITEM = R.layout.item_course_list;

    private final static int RES_ID_NUM = R.id.layout_course_number_tv;
    private final static int RES_ID_DATE = R.id.item_course_list_data_tv;
    private final static int RES_ID_TIME = R.id.item_course_list_time_tv;
    private final static int RES_ID_CONTENT = R.id.layout_course_explaincontent_tv;


    @BindView(RES_ID_LIST)
    ListView listView;
    @BindView(RES_ID_NUM)
    TextView tvNum;
    @BindView(RES_ID_CONTENT)
    TextView tvContent;

    private CourseActivity $this;
    private ArrayList<PeriodBean> periodList;
    private QuickTypeAdapter<PeriodBean> periodAdapter;
    private String courseId;

    @Override
    protected void init() {
        super.init();
        $this = this;
        setContentView(RES_ID_ROOT);
        setRefreshResId(RES_ID_REFRESH_LAYOUT);
        periodList = new ArrayList<>();
        periodAdapter = new QuickTypeAdapter<PeriodBean>(this, periodList, RES_ID_LIST_ITEM) {
            @Override
            public void bindData4View(ViewHolder holder, final PeriodBean data, int type, int pos) {
                if (data.getState().equals("1")) {
                    holder.setText(RES_ID_NUM, "预约");
                } else {
                    holder.setText(RES_ID_NUM, "已满");
                }
                holder.setText(RES_ID_DATE, data.getDate());
                holder.setText(RES_ID_TIME, data.getTimeInDate());
                holder.setLis(RES_ID_NUM, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (data.getState().equals("1")) {
                            enrollCourse(data.getId());
                        }
                    }
                });
            }
        };

        courseId = getIntent().getStringExtra(EXTRA_ID);
        if (TextUtils.isEmpty(courseId)) {
            ToastUtil.show($this, "未找到课程");
            finish();
        }
    }

    @Override
    protected void initView() {
        super.initView();
        listView.setAdapter(periodAdapter);
    }

    @Override
    protected void loadData(boolean loadMore) {
        super.loadData(loadMore);
        if (!loadMore) {
            periodList.clear();
        }
        getCourseTimeListById(courseId);
    }

    private void getCourseTimeListById(String cid) {
        mAppHelper.apiService().getCourseTimeList(cid).enqueue(new BaseCallback<ApiResponse<CourseResponseBean>>() {
            @Override
            public void onSuccess(Call<ApiResponse<CourseResponseBean>> call, Response<ApiResponse<CourseResponseBean>> response) {
                initCourse(CourseBean.dataChange(response.body().getData()));
                initCourseTimeList(PeriodBean.dataChange(response.body().getData()));
            }
        });
    }

    private void initCourse(CourseBean courseBean) {
        tvNum.setText(courseBean.getMax_number());
        tvContent.setText(courseBean.getNotice());
    }

    private void initCourseTimeList(List<PeriodBean> courseBeans) {
        periodList.clear();
        periodList.addAll(courseBeans);
        periodAdapter.notifyDataSetChanged();
        setRefresh(false);
    }

    private void setRefresh(boolean flag) {
        if (layoutRefresh != null) {
            layoutRefresh.setRefreshing(flag);
        }
    }

    public static void startActivity(Context context, String id) {
        Intent mIntent = new Intent(context, CourseActivity.class);
        mIntent.putExtra(EXTRA_ID, id);
        context.startActivity(mIntent);
    }

    //预约集体课
    private void enrollCourse(String courseId) {
        mAppHelper.apiService().enrollCourse(courseId).enqueue(new Callback<ApiResponse<String>>() {
            @Override
            public void onResponse(Call<ApiResponse<String>> call, Response<ApiResponse<String>> response) {
                if (response.isSuccessful()) {
                    ToastUtil.show($this, response.body().getMsg());
                }
                getCourseTimeListById($this.courseId);
            }

            @Override
            public void onFailure(Call<ApiResponse<String>> call, Throwable t) {
                t.getMessage();
            }
        });
    }
}
